<template>
  <div class="bigbox">
    <!-- 表头盒子 -->
    <div class="leftbox">
      <!-- 树节点过滤 -->
      <div>
        <el-input
          placeholder="输入关键字进行过滤"
          size="small"
          v-model="filterText"
        >
        </el-input>
      </div>
      <!-- 树组件 -->
      <div style="margin-top: 15px">
        <el-tree
          ref="tree"
          :props="props"
          :highlight-current="true"
          @node-click="handleNodeClick"
          :filter-node-method="filterNode"
          :load="loadNode"
          lazy
        >
        </el-tree>
      </div>
    </div>
    <!-- 盒子主体 -->
    <div class="bodybox">
      <!-- 新增按钮 -->
      <div class="top">
        <div class="title">视频Token配置</div>
        <button class="buttom" @click="submit('zen')">
          <i class="el-icon-plus" style="font-size: 12px; color: #fff"></i>新增
        </button>
      </div>
      <!-- 循环图片盒子 -->
      <div class="formbox">
        <div class="littlebox" v-for="item in entity" :key="item.id">
          <span class="title" :style="{ fontSize: '18px' }">{{
            item.orgName || item.manageName
          }}</span>
          <div class="boxbtn">
            <el-button type="text" style="color: red" @click="del(item.id)"
              ><i class="el-icon-delete"></i
            ></el-button>
            <!-- <el-button type="text" @click="edit(item.id)">编辑</el-button> -->
          </div>
          <div class="contain">
            <el-row>
              <el-col :span="8">接入方式:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb' }">{{
                  item.type == 'YS'
                    ? '萤石'
                    : item.type == 'LC'
                    ? '乐橙'
                    : item.type == 'HK'
                    ? '海康'
                    : ''
                }}</span></el-col
              >
            </el-row>
            <el-row :style="{ height: '42px' }">
              <el-col :span="8">APPkey:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb', 'word-wrap': 'break-word' }">{{
                  item.appKey
                }}</span></el-col
              >
            </el-row>
            <el-row :style="{ height: '42px' }">
              <el-col :span="8">appSecret:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb', 'word-wrap': 'break-word' }">{{
                  item.appSecret
                }}</span></el-col
              >
            </el-row>
            <el-row :style="{ height: '63px' }">
              <el-col :span="8">accessToken:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb', 'word-wrap': 'break-word' }">{{
                  item.accessToken
                }}</span></el-col
              >
            </el-row>
            <el-row>
              <el-col :span="8">令牌失效时间:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb' }">{{
                  item.expireDate
                }}</span></el-col
              >
            </el-row>

            <el-row>
              <el-col :span="8">上次检查时间:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb' }">{{
                  item.checkTime
                }}</span></el-col
              >
            </el-row>
          </div>
        </div>
      </div>
      <!-- 新增/修改弹框 -->
      <div>
        <el-dialog
          :show-close="false"
          :visible.sync="dialogVisible"
          width="50%"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :center="true"
        >
          <div class="dialog">
            <div class="title">Token信息</div>
            <!-- 企业下拉 -->
            <el-form
              ref="dialogForm"
              :rules="rules"
              :model="dialogForm"
              label-width="110px"
            >
              <el-row :gutter="40">
                <el-col :span="12">
                  <el-form-item label="企业名称" prop="orgName">
                    <el-select
                      disabled
                      v-model="dialogForm.orgName"
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="(item, index) in options"
                        :key="index"
                        :label="item.orgName"
                        :value="item.orgNo"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="接入方式" prop="platform">
                    <el-select
                      v-model="dialogForm.platform"
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="(item, index) in accessMethod"
                        :key="index.value"
                        :label="item.text"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="40">
                <el-col :span="12">
                  <el-form-item label="appKey" prop="appKey">
                    <el-input
                      v-model="dialogForm.appKey"
                      placeholder="请输入"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="appSecret" prop="appSecret">
                    <el-input
                      v-model="dialogForm.appSecret"
                      placeholder="请输入"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="40">
                <el-col :span="12">
                  <el-form-item label="accessToken" prop="accessToken">
                    <el-input
                      v-model="dialogForm.accessToken"
                      placeholder="请输入"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-button
                    :style="{
                      width: '208px',
                      height: '32px',
                      marginTop: '4px'
                    }"
                    @click="getToken"
                    >获取AccessToken</el-button
                  >
                </el-col>
              </el-row>
              <el-row :gutter="40">
                <el-col :span="12">
                  <el-form-item label="令牌失效时间" props="expireDate">
                    <el-date-picker
                      v-model="dialogForm.expireDate"
                      type="datetime"
                      placeholder="选择日期时间"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="上次检查时间" props="lastCheckDate">
                    <el-date-picker
                      v-model="dialogForm.lastCheckDate"
                      type="datetime"
                      placeholder="选择日期时间"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <!-- 弹窗内容 -->
          </div>
          <div style="margin-top: 10px; text-align: center">
            <button
              @click="quxiao"
              style="
                width: 60px;
                height: 30px;
                margin-right: 10px;
                background-color: #fff;
                border: 1px solid #ccc;
              "
            >
              取 消
            </button>
            <button
              @click="sure"
              style="
                width: 60px;
                height: 30px;
                background-color: #3b8eea;
                color: #fff;
                border: none;
              "
            >
              确 定
            </button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getVideoListApi,
  saveVideoListApi,
  getYSTokenApi,
  getLCTokenApi,
  getVideoTypeSelectApi,
  delVideoListApi,
  getHKTokenApi
} from '@/api/videoConfig'
import { gettreelist, gettreexiaji } from '../../api/userscreen'
import dayjs from 'dayjs'
export default {
  name: 'CallPoliceIndex',
  data() {
    return {
      value: '',
      entity: [],
      options: [],
      filterText: '',
      dialogForm: {},
      accessMethod: [],
      rules: {
        platform: [{ required: true, message: '请先选择接入方式' }],
        appKey: [{ required: true, message: '请输入appKey', trigger: 'blur' }],
        appSecret: [
          { required: true, message: '请输入appSecret', trigger: 'blur' }
        ],
        accessToken: [{ required: true, message: '请输入accessToken' }],
        expireDate: [{ required: true, message: '请选择令牌失效时间' }],
        lastCheckDate: [{ required: true, message: '请选择上次检查时间' }]
      },
      props: {
        label: 'bizOrgName',
        disabled: 'disabled'
      },
      dialogVisible: false,
      isbianji: ''
    }
  },
  created() {
    this.getVideoList()
    this.getVideoTypeSelect()
  },
  async mounted() {},
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    // 新增/修改
    submit(val) {
      this.isbianji = val
      if (!this.dialogForm.orgName) {
        return this.$message.warning('请先选择企业')
      }
      this.dialogVisible = true
    },
    //  取消/弹框关闭
    quxiao() {
      this.dialogVisible = false
      this.isshow = false
      this.$refs.dialogForm.resetFields()
    },
    // 确认修改并保存
    async sure() {
      await this.$refs.dialogForm.validate()
      const res = await saveVideoListApi(this.dialogForm)
      if (res.code === 200) {
        this.$message.success('保存成功')
        this.dialogVisible = false
        await this.getVideoList()
        this.$refs.dialogForm.resetFields()
      } else {
        this.$message.error('保存失败')
      }
      console.log(res)
    },
    // 删除头像
    // 树节点点击
    async handleNodeClick(val) {
      console.log(val)
      this.dialogForm.orgName = val.bizOrgName
      this.dialogForm.orgNo = val.bizOrgNo
      this.options = [val]
      this.value = val.bizOrgNo
      this.getVideoList()
    },
    // 树组件懒加载
    async loadNode(node, resolve) {
      const res = await gettreelist()
      if (node.level === 0) {
        // 初始的级数（最顶层）
        return resolve(res.data?.list) // 最顶层数据渲染为region
      }
      const ress = await gettreexiaji({ orgNo: node.data.bizOrgNo })
      if (node.level === 1) {
        return resolve(ress.data?.list)
      } else if (node.level === 2) {
        return resolve([])
      }
    },
    // 树节点过滤
    filterNode(value, data) {
      if (!value) return true
      return data.bizOrgName.indexOf(value) !== -1
    },
    async getToken() {
      await this.$refs.dialogForm.validateField('platform')
      await this.$refs.dialogForm.validateField('appKey')
      await this.$refs.dialogForm.validateField('appSecret')
      if (this.dialogForm.platform === 'YS') {
        const res = await getYSTokenApi({
          appKey: this.dialogForm.appKey,
          appSecret: this.dialogForm.appSecret
        })
        // console.log(res)
        if (res.data.code == 200) {
          this.$set(this.dialogForm, 'accessToken', res.data.data.accessToken)
          // this.dialogForm.accessToken = res.data.data.accessToken
          this.dialogForm.expireTime = res.data.data.expireTime
          this.dialogForm.expireDate = dayjs(
            new Date(res.data.data.expireTime)
          ).format('YYYY-MM-DD HH:mm:ss')
          this.dialogForm.lastCheckDate = dayjs(new Date()).format(
            'YYYY-MM-DD HH:mm:ss'
          )
        } else {
          this.$message.error(res.data.msg)
        }
      } else if (this.dialogForm.platform === 'HK') {
        const res = await getHKTokenApi({
          appKey: this.dialogForm.appKey,
          appSecret: this.dialogForm.appSecret
        })
        // console.log(res)
        if (res.code == 200) {
          this.$set(this.dialogForm, 'accessToken', res.data.entity.token)
          // this.dialogForm.accessToken = res.data.data.accessToken
          this.dialogForm.expireTime = res.data.entity.expireTime
          this.dialogForm.expireDate = dayjs(
            new Date(res.data.entity.expireTime)
          ).format('YYYY-MM-DD HH:mm:ss')
          this.dialogForm.lastCheckDate = dayjs(new Date()).format(
            'YYYY-MM-DD HH:mm:ss'
          )
        } else {
          this.$message.error(res.data.msg)
        }
      } else if (this.dialogForm.platform === 'LC') {
        const res = await getLCTokenApi({
          appKey: this.dialogForm.appKey,
          appSecret: this.dialogForm.appSecret
        })
        console.log(res)
        if (res.code == 200) {
          this.$set(this.dialogForm, 'accessToken', res.data.entity.token)
          this.dialogForm.expireTime = res.data.entity.expireTime
          this.dialogForm.expireDate = dayjs(
            new Date(res.data.entity.expireTime)
          ).format('YYYY-MM-DD HH:mm:ss')
          this.dialogForm.lastCheckDate = dayjs(new Date()).format(
            'YYYY-MM-DD HH:mm:ss'
          )
        } else {
          this.$message.error('获取失败,请检查appKey和appSerect是否有误')
        }
      }
    },
    async getVideoList() {
      const res = await getVideoListApi({
        orgNo: this.value || '',
        pageNo: 0,
        pageSize: 10
      })
      this.entity = res?.data.list
      console.log(res)
    },
    async getVideoTypeSelect() {
      const res = await getVideoTypeSelectApi()
      console.log(res)
      this.accessMethod = res?.data.list
    },
    async del(id) {
      this.$confirm('此操作将永久删除该档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await delVideoListApi(id)
            this.$message.success('删除成功')
          } catch (err) {
            this.$message.success('删除失败')
          }
          this.getVideoList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      this.getVideoList()
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.bigbox {
  background-color: #f1f1f1;
  min-height: 100%;
  padding: 10px;
  display: flex;
  .leftbox {
    width: 300px;
    background-color: #fff;
    padding: 20px;
  }
  .bodybox {
    flex: 1;
    margin-left: 10px;
    background-color: #fff;
    padding: 20px;
    .top {
      display: flex;
      justify-content: space-between;
      text-align: right;
      padding-bottom: 10px;
      margin-bottom: 20px;
      border-bottom: 1px solid #bbbbbb;
      .buttom {
        width: 70px;
        height: 30px;
        background-color: #1890ff;
        color: #fff;
        border-radius: 3px;
        border: none;
      }
    }
    .formbox {
      margin-top: 10px;
      display: flex;
      flex-wrap: wrap;
      .littlebox {
        position: relative;
        margin-right: 5%;
        margin-bottom: 20px;
        width: 28%;
        border: 1px solid #1890ff;
        border-radius: 3px;
        padding: 10px;
        .boxbtn {
          position: absolute;
          top: 10px;
          right: 20px;
        }
        .contain {
          margin-top: 40px;
          padding-left: 21px;
          .el-row {
            margin-bottom: 30px;
          }
        }
      }
    }
    .dialog {
      .title {
        font-size: 28px;
        margin-bottom: 30px;
      }
      .text {
        font-size: 12px;
        color: #3b8eea;
      }
      .el-row {
        margin-bottom: 20px;
      }
    }
  }
}

::v-deep .el-select {
  width: 100%;
}
::v-deep .el-input {
  height: 32px;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 32px;
}
.el-select-dropdown__item {
  padding-left: 10px;
}
::v-deep .el-date-editor {
  width: 100%;
}
button {
  cursor: pointer;
}
</style>
